﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using PBOffice.DAL;

namespace PBOffice.BLL
{
   public class PositionManager
   {
        #region 查询部门职位信息（根据部门编号）
       /// <summary>
        /// 查询部门职位信息（根据部门编号）
        /// </summary>
        /// <param name="depNo">部门编号</param>
        /// <returns></returns>
       public List<v_DepPosition> ShowPosInfoByDepNo(int depNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var pos = from position in myOffice.v_DepPosition
                          where position.DepNo==depNo
                          select position;
                return pos.ToList();
            }
        } 
        #endregion

        #region 查询职位信息（根据机构编号）
       /// <summary>
       /// 查询职位信息（根据机构编号）
       /// </summary>
       /// <param name="depNo"></param>
       /// <returns></returns>
       public List<v_DepPosition> ShowPosInfoByOrganNo(int organNo)
       {
           using (MyOfficeEntities myOffice = new MyOfficeEntities())
           {
               var pos = from position in myOffice.v_DepPosition
                         where position.OrganNo == organNo
                         select position;
               return pos.ToList();
           }
       }  
       #endregion

        #region 查询职位信息（根据职位名称）
        /// <summary>
        /// 查询职位信息（根据职位名称）
        /// </summary>
        /// <param name="posId"></param>
        /// <returns></returns>
       public List<v_DepPosition> ShowPosInfoByPosId(string posId)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var pos = from position in myOffice.v_DepPosition
                          where position.PosId.Contains(posId)
                          select position;
                return pos.ToList();
            }
        }  
        #endregion

        #region 添加职位信息
        /// <summary>
        /// 添加职位信息
        /// </summary>
        /// <param name="p">职位参数</param>
        /// <returns></returns>
       public bool AddPosInfo(Position p,int depNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                Position pos = new Position
                {
                    PosId = p.PosId,
                };
                pos.Department = myOffice.Departments.SingleOrDefault(dep=>dep.DepNo==depNo);
                myOffice.Positions.AddObject(pos);   //添加职位信息
                return myOffice.SaveChanges() > 0;   //添加成功，返回true；否则，返回false
            }
        } 
        #endregion

        #region 修改职位信息（根据职位编号）
        /// <summary>
        /// 修改职位信息（根据职位编号）
        /// </summary>
        /// <param name="p">职位对象</param>
        /// <returns></returns>
        public bool ChangePosInfo(Position p)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var pos1 = from position in myOffice.Positions
                           where position.PosNo == p.PosNo
                           select position;
                pos1.First();
                Position pos = (Position)pos1;
                pos.PosId = p.PosId;
                pos.Department.DepNo = p.Department.DepNo;
                return myOffice.SaveChanges() > 0;         //修改职位信息，成功，返回true；否则，返回false
            }
        } 
        #endregion

        #region 删除职位信息（根据职位编号）
        /// <summary>
        /// 删除职位信息（根据职位编号）
        /// </summary>
        /// <param name="posNo">职位编号</param>
        /// <returns></returns>
        public bool DeletePosInfo(int posNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var pos1 = from position in myOffice.Positions
                           where position.PosNo == posNo
                           select position;               //根据编号查询职位信息
                pos1.First();                             //返回查询结果中的第一个元素
                Position pos = (Position)pos1;            //类型转换
                myOffice.Positions.DeleteObject(pos);     //删除职位信息
                return myOffice.SaveChanges() > 0;          //删除成功，返回true；否则，返回false
            }
        } 
        #endregion
    }
}
